##PBAR_Zspec_Load_Ver2.py
# loads the PBAR zspec data
#
#   4/1/2013, John Kwong
#   4/17/2013, Added stuff
#   4/18/2013, modified so that data groups can have only one dataset
#   4/26/2013, replaced with PBAR_Zspec_LoadAndProcessData.py

import csv
import numpy as np
import numpy.matlib
import datetime
import time

# location of the data
basepath = r'N:\My Documents\Projects\PBAR\data'
basepath = r'C:\Users\jkwong\Documents\Work\PBAR\data'

plotColors = ['r', 'b', 'g', 'm', 'c', 'y', 'k'] * 10
lineStyles = ['-', '-.', ':', '_', '|'] *  10
markerTypes = ['.', 'o', 'v', '^', '<', '>', '1', '2', '3', '4', 's', 'p', '*', 'h', 'H', '+', 'x', 'D', 'd']
markerTypes = markerTypes * 2

# Read in summary file
summaryFile = 'C:\Users\jkwong\Documents\Work\PBAR\data\datasetSummaryOLD.txt'
fid = open(summaryFile, 'r');
csvReaderObj = csv.reader(fid, delimiter = '\t')

datasetName = []
acquisitionTime = []
timeStamp = []
timeNum = []
header = []

for lineIn in csvReaderObj:
    datasetName.append(lineIn[0])
    timeStamp.append(lineIn[1])
    acquisitionTime.append(lineIn[2])
    timeNum.append(lineIn[3])
    header.append(lineIn[4])

acquisitionTime = np.array(acquisitionTime)
acquisitionTime = acquisitionTime.astype(float)
datasetName = np.array(datasetName)
timeStamp = np.array(timeStamp)
timeNum = np.array(timeNum)
timeNum = timeNum.astype(float)
header = np.array(header)

# read in the radiography data
radMap = np.genfromtxt(basepath + '\\' + 'arrayResponse.txt', delimiter = '\t', skip_header = 1, dtype = str)

# load all the radiography data fount in radMap and put into a dictionary
datRad = dict()
for ii in range(0, radMap.shape[0]):
    dsName = radMap[ii,0]
    if (radMap[ii,3] != ''):
        radName = radMap[ii,3]
        fullFileName = basepath + '\\' + 'Array-Response' + '\\' + radName + '.csv'
        temp = np.genfromtxt(fullFileName, delimiter = '\t', skip_header = 1, dtype = str)
        datRad[dsName] = temp.astype(float)

# make map from z-spec numbers to radiography detector numbers
zspecToRad = dict()
for i in range(0,136):
    zspecToRad[i] = np.array((0, 1, 2 ,3)) + i*4

# define list of good/bad detectors
t = np.array([15, 20, 26, 31, 33, 39, 40, 44, 53, 56, 62, 68, 76, 80, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137])
badDetectors = np.concatenate((np.arange(1,9), t)) - 1
temp = np.zeros(137)
temp[badDetectors] = True
temp = temp.astype(bool)
goodDetectors = ~temp
goodDetectorsList = np.array(np.where(goodDetectors))[0]
badDetectorsList = np.array(np.where(badDetectors))[0]

# Make list of file names that will be loaded
fullfilenameList = []
filenameList = []
for ii in range(95,100):
    filenameList.append('dx' + '%02d' % ii)
for ii in range(0,65):
    filenameList.append('dy' + '%02d' % ii)
filenameList = np.array(filenameList)

for ii in range(len(filenameList)):
    fullfilenameList.append(basepath + '\\' + filenameList[ii] + '.csv')
fullfilenameList = np.array(fullfilenameList)

# Get the information about the dataset
datasetDescription = [] # header
datasetTimeStr = [] # dataset time stamp string
datasetTime = [] # dataset time stamp string
datasetTimeNum = [] # epoch start time
datasetAcquisitionTime = [] # acquisition start time

for ii in range(0, len(filenameList)):
    index = np.where(datasetName == filenameList[ii])[0][0]
    datasetDescription.append(header[index])
    datasetTimeStr.append(timeStamp[index])
    datasetTimeNum.append(timeNum[index])
    datasetTime.append(time.localtime(timeNum[index]))
    datasetAcquisitionTime.append(acquisitionTime[index])

datasetDescription = np.array(datasetDescription)
datasetTimeStr = np.array(datasetTimeStr)
datasetTimeNum = np.array(datasetTimeNum)
datasetAcquisitionTime = np.array(datasetAcquisitionTime)

# Load the Zspec data
dat = list()
for ii in range(len(fullfilenameList)):
    temp = np.genfromtxt(fullfilenameList[ii], \
                             delimiter=',', \
                             skip_header = 0, \
                             skip_footer = 0, \
                             dtype = 'uint32')
    if (temp.shape[0] == 257):  # shave off the table labels if present
        temp = temp[1:,:]
    if (temp.shape[1] == 138):
        temp = temp[:,1:-1]
    dat.append(temp)

### LISTS OF DATASETS GROUPS
datasetGroups = dict()
datasetGroups['CC'] = ('dx95', 'dy00', 'dy01', 'dy05', \
                       'dy10', 'dy15', 'dy16', 'dy22', 'dy35', 'dy36')
datasetGroups['Pb'] = ('dx96', 'dx97', 'dx98', 'dx99')
datasetGroups['Fe'] = ('dy02', 'dy03', 'dy04')
datasetGroups['Al'] = ('dy06', 'dy07', 'dy08', 'dy09')

datasetGroups['Pb3Fe'] = ('dy11', 'dy12', 'dy13', 'dy14')
datasetGroups['Pb3Al'] = ('dy17', 'dy18', 'dy19', 'dy20', 'dy21')
datasetGroups['Pb4Fe'] = ('dy23', 'dy24', 'dy25', 'dy26', 'dy27', 'dy28')
datasetGroups['Pb4Al'] = ('dy37', 'dy38', 'dy39', 'dy40', 'dy41', 'dy42', 'dy43')

datasetGroups['PbALL'] = datasetGroups['Pb'] + \
                         datasetGroups['Pb3Fe'] + \
                         datasetGroups['Pb3Al'] + \
                         datasetGroups['Pb4Fe'] + \
                         datasetGroups['Pb4Al']
datasetGroups['PbNOT'] = datasetGroups['Fe'] + \
                         datasetGroups['Al']

# low stats data sets
datasetGroups['PbLS'] = ('dy45', 'dy46', 'dy47', 'dy48', 'dy49', \
                       'dy50', 'dy51', 'dy52', 'dy53', 'dy54')
datasetGroups['FeLS'] = ('dy55', 'dy56', 'dy57', 'dy58', 'dy59')
datasetGroups['AlLS'] = ('dy60', 'dy61', 'dy62', 'dy63', 'dy64')

datasetGroups['PbLS'] = ('dy45', 'dy46', 'dy47', 'dy48', 'dy49', \
                       'dy50', 'dy51', 'dy52', 'dy53', 'dy54')
datasetGroups['FeLS'] = ('dy55', 'dy56', 'dy57', 'dy58', 'dy59')

datasetGroups['Pb5_8'] = ('dx96', 'dx99')
datasetGroups['Fe12'] = 'dy04'
datasetGroups['Al30'] = 'dy09'

datasetGroups['PbLSALL'] = datasetGroups['PbLS']
datasetGroups['PbLSNOT'] = datasetGroups['FeLS'] + \
                         datasetGroups['AlLS']

groupNames = datasetGroups.keys()
datasetGroupsIndices = dict()

for ii in range(0, len(groupNames)):
    temp = []
    if isinstance(datasetGroups[groupNames[ii]], str):
        datasetGroupsIndices[groupNames[ii]] = np.where(filenameList == datasetGroups[groupNames[ii]])[0]
    else:
        for jj in range(0, len(datasetGroups[groupNames[ii]])):
            index = np.where(filenameList ==  datasetGroups[groupNames[ii]][jj] )[0][0]
            temp.append(index)
        datasetGroupsIndices[groupNames[ii]] = np.array(temp)
